﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GGGK.Film.IRepository;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;

namespace GGGK.Film.Repository
{
    public class BaseRepository<T> : BaseQueryRepository,IBaseRepository<T> where T : class, new()
    {
        private readonly DbContext dbContext;

        public BaseRepository(DbContext dbContext)
        {
            this.dbContext = dbContext;
        }

        public void Add(T t)
        {
            dbContext.Set<T>().Add(t);
        }

        public void Delete(T t)
        {
            dbContext.Set<T>().Remove(t);
        }

        public T? Get(Expression<Func<T, bool>> whereLambda)
        {
            return dbContext.Set<T>().Where(whereLambda).FirstOrDefault();
        }

        public IQueryable<T> Query(Expression<Func<T, bool>> whereLambda)
        {
            return dbContext.Set<T>().Where(whereLambda);
        }

        public bool SaveChanges()
        {
            return dbContext.SaveChanges()>0;
        }

        public void Update(T t)
        {
            dbContext.Set<T>().Update(t);
        }
    }
}
